package com.github.binarywang.demo.wx.mp.controller;

import com.github.binarywang.demo.wx.mp.beans.HaoZhu;
import com.github.binarywang.demo.wx.mp.beans.WxMultiConfigDto;
import com.github.binarywang.demo.wx.mp.dto.Result;
import com.github.binarywang.demo.wx.mp.service.Haozhuservice;
import com.github.binarywang.demo.wx.mp.service.wxservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
@Api(tags = "号主管理模块")
@Controller
public class HaoZhuController {
    @Autowired
    Haozhuservice hs;
    @Autowired
    wxservice ws;

    @ResponseBody
    @PostMapping("/getall")
    @ApiOperation(value="获取所有号主",notes="请求参数role 1:流量主 2：广告主 3：管理员，会返回号主的信息")
    public Result<List<HaoZhu>> getAllByrole(@RequestParam(value = "role", required = true)String role) {
        List<HaoZhu> alls = hs.getAllByrole(role);
        return Result.success(alls);
    }

    @ResponseBody
    @PostMapping("/deleteHZById")
    @ApiOperation(value="根据号主id删除号主",notes="hao_zhu_id为号主Id")
    public Result deleteHZById(@RequestParam(value = "hao_zhu_id", required = true) String hao_zhu_id) {
        hs.deleteHZById(hao_zhu_id);
        return Result.success("删除成功");
    }

    @ResponseBody
    @PostMapping("/countGZHNum")
    @ApiOperation(value="查看公众号总数",notes="没有参数")
    public Result countGZHNum() {
        int num = ws.countGZHNum();
        return Result.success(num);
    }
    @ResponseBody
    @PostMapping("/countHZNumByrole")
    @ApiOperation(value="查看号主总数",notes="请求参数role 1:流量主 2：广告主 3：管理员，会返回号主的信息")
    public Result countHZNumByrole(@RequestParam(value = "role", required = true)String role) {
        int num = hs.countHZNumByrole(role);
        return Result.success(num);
    }

    @ResponseBody
    @PostMapping("/statusCTO")
    @ApiImplicitParams({
        @ApiImplicitParam(name="client_id",value="公众号client_id"),
        @ApiImplicitParam(name="status",value="公众号状态"),
    })
    @ApiOperation(value="控制公众号状态",notes="通过公众号client_id控制，status: available 表示可用，disable表示禁用")
    public Result<WxMultiConfigDto> statusCTO(@RequestParam(value = "client_id", required = true)String client_id,@RequestParam(value = "status", required = true)String status) {
        ws.statusCTO(client_id,status);
        return Result.success(status);
    }



    //以下只有用户权限

    @ResponseBody
    @PostMapping("/getGZHbyId")
    @ApiOperation(value="获取号主的公众号",notes="id为号主HaoZhuId")
    public Result<List<WxMultiConfigDto>> getGZHbyId(@RequestParam(value = "hao_zhu_id", required = true) String hao_zhu_id) {
        List<WxMultiConfigDto> wc=ws.getGZHbyId(hao_zhu_id);
        if(wc!=null){
            return Result.success(wc);
        }
        return Result.success(null);

    }
    @ResponseBody
    @PostMapping("/getsingleGZHbyId")
    @ApiOperation(value="获取公众号详情",notes="clientId为公众号Id")
    public Result<WxMultiConfigDto> getsingleGZHbyId(@RequestParam(value = "client_id", required = true) String client_id) {
        WxMultiConfigDto wc=ws.getsingleGZHbyId(client_id);
        return Result.success(wc);
    }


    @ResponseBody
    @PostMapping("/delGZH")
    @ApiOperation(value="号主删除公众号",notes="client_id为公众号clientid")
    public Result delGZHbyclient_id(@RequestParam(value = "client_id", required = true)String client_id){
        ws.delGZHbyclient_id(client_id);
        return Result.success("删除的成功");
    }
    @ResponseBody
    @PostMapping("/updateGZH")
        @ApiImplicitParams({
        @ApiImplicitParam(name="client_id",value="根据用户id修改", required = true),
        @ApiImplicitParam(name="appid",value="公众号id"),
        @ApiImplicitParam(name="appsecret",value="公众号秘钥"),
        @ApiImplicitParam(name="token",value="公众号token"),
        @ApiImplicitParam(name="url",value="公众号服务器地址"),
    })
    @ApiOperation(value="号主修改公众号",notes="根据用户id修改信息")
    public Result updateGZHbyclientid(@RequestParam(value = "client_id", required = true)String client_id,
                                   @RequestParam(value = "appid",required = false)String appid,
                                   @RequestParam(value = "appsecret",required = false)String appsecret,
                                   @RequestParam(value = "token",required = false)String token,
                                   @RequestParam(value = "url",required = false)String url) {
        ws.updateGZHbyclientid(client_id,appid, appsecret,token,url);
        return Result.success("修改成功");
    }
//    @ResponseBody
//    @PostMapping("/updateHZById")
//    @ApiOperation(value="根据号主id修改号主信息",notes="HaoZhuId为号主Id")
//    public Result updateHZById(@RequestBody HaoZhu haoZhu) {
//        hs.updateHZById(haoZhu);
//        return ResultUtil.success("修改成功");
//    }
}
